package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import com.wpen.unit.infra.repository.job.BatchExecRedologRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.BatchExecRedologMapper;
import com.wpen.unit.infra.mybatis.job.po.BatchExecRedologPo;
import com.wpen.unit.infra.mybatis.job.po.BatchExecRedologPo.BatchExecRedologColumns;
import com.wpen.unit.utils.StringUtil;

/**
 * 批次执行日志维护.
 * 
 * @author Wang Peng
 * @date 2023-01-18
 *
 */
@Repository
@Transactional
public class BatchExecRedologRepositoryImpl extends ServiceImpl<BatchExecRedologMapper, BatchExecRedologPo> implements BatchExecRedologRepository {
    
    @Override
    public long count(BatchExecRedologPo batchExecRedologPo){
		QueryWrapper<BatchExecRedologPo> queryWrapper = createQuery(batchExecRedologPo);
		return super.count(queryWrapper);
    }

	@Override
    public List<BatchExecRedologPo> queryList(int index, int size, BatchExecRedologPo batchExecRedologPo){
        QueryWrapper<BatchExecRedologPo> queryWrapper = createQuery(batchExecRedologPo);
		if (size > 0) {
			Page<BatchExecRedologPo> page = new Page<>(index, size, false);
			return super.page(page, queryWrapper).getRecords();
		}
		//queryWrapper.orderBy(true, false, BatchExecRedologColumns.ID.name());
		return getBaseMapper().selectList(queryWrapper);
    }
    
    private QueryWrapper<BatchExecRedologPo> createQuery(BatchExecRedologPo batchExecRedologPo) {
        QueryWrapper<BatchExecRedologPo> queryWrapper = Wrappers.query();
        if (batchExecRedologPo.getId() != null) {
            queryWrapper.eq(BatchExecRedologColumns.ID.name(), batchExecRedologPo.getId());
        }
        if (batchExecRedologPo.getBatchLogId() != null) {
            queryWrapper.eq(BatchExecRedologColumns.BATCH_LOG_ID.name(), batchExecRedologPo.getBatchLogId());
        }
        if (StringUtil.isNotEmpty(batchExecRedologPo.getBizCode())) {
            queryWrapper.eq(BatchExecRedologColumns.BIZ_CODE.name(), batchExecRedologPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(batchExecRedologPo.getBatchCode())) {
            queryWrapper.eq(BatchExecRedologColumns.BATCH_CODE.name(), batchExecRedologPo.getBatchCode());
        }
        return queryWrapper;
    }
    
    @Override
    public BatchExecRedologPo queryFirst(BatchExecRedologPo batchExecRedologPo) {
        QueryWrapper<BatchExecRedologPo> queryWrapper = Wrappers.query();
        if (batchExecRedologPo.getId() != null) {
            queryWrapper.eq(BatchExecRedologColumns.ID.name(), batchExecRedologPo.getId());
        }
        if (batchExecRedologPo.getBatchLogId() != null) {
            queryWrapper.eq(BatchExecRedologColumns.BATCH_LOG_ID.name(), batchExecRedologPo.getBatchLogId());
        }
        if (StringUtil.isNotEmpty(batchExecRedologPo.getBizCode())) {
            queryWrapper.eq(BatchExecRedologColumns.BIZ_CODE.name(), batchExecRedologPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(batchExecRedologPo.getBatchCode())) {
            queryWrapper.eq(BatchExecRedologColumns.BATCH_CODE.name(), batchExecRedologPo.getBatchCode());
        }
		//queryWrapper.orderBy(true, false, BatchExecRedologPoColumns.ID.name());
		return super.getOne(queryWrapper);
    }
    
    @Override
    public BatchExecRedologPo queryById(Serializable id) {
        return super.getById(id);
    }
    
    @Override
    public boolean insert(BatchExecRedologPo batchExecRedologPo) {
//        batchExecRedologPo.setVersion(1);
        batchExecRedologPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(batchExecRedologPo);
    }

    @Override
    public boolean insertBatch(List<BatchExecRedologPo> listBatchExecRedologPos) {
        int rs = getBaseMapper().insertBatchSomeColumn(listBatchExecRedologPos);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(BatchExecRedologPo batchExecRedologPo) {
        UpdateWrapper<BatchExecRedologPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(BatchExecRedologColumns.ID.name(), batchExecRedologPo.getId());
//        updateWrapper.eq(BatchExecRedologColumns.VERSION.name(), batchExecRedologPo.getVersion());
//        batchExecRedologPo.setVersion(batchExecRedologPo.getVersion() + 1);
        boolean rs = super.update(batchExecRedologPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(ucpBatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<BatchExecRedologPo> updateWrapper = Wrappers.update();
		updateWrapper.in(BatchExecRedologColumns.ID.name(), (Object[]) ids);
		return super.remove(updateWrapper);
    }
    
//    public BatchExecRedologPo queryByExample(BatchExecRedologPo batchExecRedologPo) {
//        QueryWrapper<BatchExecRedologPo> queryWrapper = Wrappers.query();
//        if (batchExecRedologPo.getId() != null) {
//            queryWrapper.eq(BatchExecRedologColumns.ID.name(), batchExecRedologPo.getId());
//        }
//        if (batchExecRedologPo.getBatchLogId() != null) {
//            queryWrapper.eq(BatchExecRedologColumns.BATCH_LOG_ID.name(), batchExecRedologPo.getBatchLogId());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getBizCode())) {
//            queryWrapper.eq(BatchExecRedologColumns.BIZ_CODE.name(), batchExecRedologPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getBatchCode())) {
//            queryWrapper.eq(BatchExecRedologColumns.BATCH_CODE.name(), batchExecRedologPo.getBatchCode());
//        }
//        if (batchExecRedologPo.getBatchTriggerTime() != null) {
//            queryWrapper.eq(BatchExecRedologColumns.BATCH_TRIGGER_TIME.name(), batchExecRedologPo.getBatchTriggerTime());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getBatchType())) {
//            queryWrapper.eq(BatchExecRedologColumns.BATCH_TYPE.name(), batchExecRedologPo.getBatchType());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getScheduleType())) {
//            queryWrapper.eq(BatchExecRedologColumns.SCHEDULE_TYPE.name(), batchExecRedologPo.getScheduleType());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getExecDate())) {
//            queryWrapper.eq(BatchExecRedologColumns.EXEC_DATE.name(), batchExecRedologPo.getExecDate());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getExecStatus())) {
//            queryWrapper.eq(BatchExecRedologColumns.EXEC_STATUS.name(), batchExecRedologPo.getExecStatus());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getExecResult())) {
//            queryWrapper.eq(BatchExecRedologColumns.EXEC_RESULT.name(), batchExecRedologPo.getExecResult());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getStartupTime())) {
//            queryWrapper.eq(BatchExecRedologColumns.STARTUP_TIME.name(), batchExecRedologPo.getStartupTime());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getEndTime())) {
//            queryWrapper.eq(BatchExecRedologColumns.END_TIME.name(), batchExecRedologPo.getEndTime());
//        }
//        if (StringUtil.isNotEmpty(batchExecRedologPo.getCreateTime())) {
//            queryWrapper.eq(BatchExecRedologColumns.CREATE_TIME.name(), batchExecRedologPo.getCreateTime());
//        }
//		Page<BatchExecRedologPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, BatchExecRedologPoColumns.ID.name());
//    }

}
